package com.byt.framlib.commonwidget;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import androidx.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.byt.framlib.R;
import com.byt.framlib.base.BaseActivity;

public class MyWebViewClient extends WebViewClient {

    private LoadingTip mLoadingTip;
    private WebView mWebView;
    private BaseActivity mContext;
    private VisitBindSaleJump mVisitBindSaleJump;

    public MyWebViewClient(WebView webView, BaseActivity context) {
        mWebView = webView;
        mContext = context;
    }

    public MyWebViewClient(LoadingTip loadingTip, WebView webView, BaseActivity context) {
        mLoadingTip = loadingTip;
        mWebView = webView;
        mContext = context;
    }

    public MyWebViewClient(LoadingTip loadingTip, WebView webView, BaseActivity context, VisitBindSaleJump mVisitBindSaleJump) {
        mLoadingTip = loadingTip;
        mWebView = webView;
        mContext = context;
        this.mVisitBindSaleJump = mVisitBindSaleJump;
    }

    //在开始加载网页时会回调
    @Override
    public void onPageStarted(WebView webView, String s, Bitmap bitmap) {
        super.onPageStarted(webView, s, bitmap);
        if (mLoadingTip != null) {
            mLoadingTip.setLoadingTip(LoadingTip.LoadStatus.finish);
            mWebView.setVisibility(View.VISIBLE);
        }
    }

    //加载错误的时候会回调
    @Override
    public void onReceivedError(WebView webView, int i, String s, String s1) {
        super.onReceivedError(webView, i, s, s1);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            return;
        }
        if (mLoadingTip != null) {
            mLoadingTip.setLoadingTip(LoadingTip.LoadStatus.error);
            mLoadingTip.setRealoadVisibility(false);
            mLoadingTip.setTips("抱歉,您访问的页面不存在~\n" + i + "\n错误信息:" + s);
            mLoadingTip.setTipLogo(R.drawable.ic_web_no_data, (int) mContext.getResources().getDimension(R.dimen.x600),
                    (int) mContext.getResources().getDimension(R.dimen.y600));
            mWebView.setVisibility(View.INVISIBLE);
        }
    }

    //加载错误的时候会回调
    @SuppressLint("NewApi")
    @Override
    public void onReceivedError(WebView webView, WebResourceRequest webResourceRequest, WebResourceError webResourceError) {
        super.onReceivedError(webView, webResourceRequest, webResourceError);
        if (webResourceRequest.isForMainFrame()) {
            if (mLoadingTip != null) {
                mLoadingTip.setLoadingTip(LoadingTip.LoadStatus.error);
                mLoadingTip.setRealoadVisibility(false);
                mLoadingTip.setTips("抱歉,您访问的页面不存在~\n" + webResourceError.getErrorCode() + "\n错误信息:" + webResourceError.getDescription().toString());
                mLoadingTip.setTipLogo(R.drawable.ic_web_no_data, (int) mContext.getResources().getDimension(R.dimen.x600),
                        (int) mContext.getResources().getDimension(R.dimen.y600));
                mWebView.setVisibility(View.INVISIBLE);
            }
        }
    }

    //加载完成的时候会回调
    @Override
    public void onPageFinished(WebView webView, String s) {

    }

    @SuppressLint("MissingPermission")
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        String tag = "tel:";
       if (url.contains("jump")) {
            Uri uri = Uri.parse(url);
            String type = uri.getQueryParameter("jump");
            if (!TextUtils.isEmpty(type)) {
                int jumpType;
                try {
                    jumpType = Integer.parseInt(type);
                } catch (Exception e) {
                    jumpType = 1;
                }
                String planId = "";
                if (jumpType == 1) {
                    planId = uri.getQueryParameter("plan_id");
                } else {
                    planId = uri.getQueryParameter("order_id");
                }
                if (mVisitBindSaleJump != null) {
                    mVisitBindSaleJump.jumpClick(jumpType, planId);
                }
            }
            if (url.startsWith("http:") || url.startsWith("https:")) {
                view.loadUrl(url);
            }
            return false;
        } else if (url.startsWith("http:") || url.startsWith("https:")) {
            view.loadUrl(url);
            return false;
        }
        return true;
    }



    @Nullable
    @Override
    public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
        return super.shouldInterceptRequest(view, request);
    }

    @Nullable
    @Override
    public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
        return super.shouldInterceptRequest(view, url);
    }

    public interface VisitBindSaleJump {
        void jumpClick(int jumpType, String jumpPlan_id);
    }
}
